﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using RpgLibrary.CharacterClasses;

namespace RpgLibrary.WorldClasses
{
    public struct Tile
    {
        public int TileIndex;
        
        public Tile(int tileIndex)
        {
            TileIndex = tileIndex;
        }
    }

    public struct PassTile
    {
        public int TileIndex;
        
        public List<Rectangle> PassRects;

        public PassTile(int tileIndex, List<Rectangle> passRects)
        {
            TileIndex = tileIndex;
            PassRects = passRects;
        }
        
        public PassTile(int tileIndex)
        {
            TileIndex = tileIndex;
            PassRects = new List<Rectangle>();
        }
    }

    public class MapLayerData
    {
        public string LayerName
        {
            get
            {
                return layerName;
            }
            set
            {
                layerName = value;
            }
        }

        public string layerName;
        public int Width;
        public int Height;
        public int TileSetIndex;
        public Tile[] Layer;
        public PassTile[] PassLayer;
        public CharacterData[] Characters;
        public bool Visible = true;
        
        private MapLayerData()
        {
        }

        public MapLayerData(string mapLayerName, int width, int height, bool visible, List<CharacterData> characters)
            : this(mapLayerName, width, height, visible, -1, -1, characters)
        {
        }
        
        public MapLayerData(string mapLayerName, int width, int height, bool visible, int tileIndex, int tileSet, List<CharacterData> characters)
        {
            LayerName = mapLayerName;
            Width = width;
            Height = height;
            Visible = visible;
            Characters = characters.ToArray();
            Layer = new Tile[height * width];
            TileSetIndex = tileSet;

            Tile tile = new Tile(tileIndex/*, tileSet*/);

            for (int y = 0; y < height; y++)
                for (int x = 0; x < width; x++)
                    SetTile(x, y, tile);

            PassLayer = new PassTile[height * width];
        }

        public void SetTile(int x, int y, int tileIndex)
        {
            Layer[y * Width + x] = new Tile(tileIndex);
        }

        public void SetTile(int x, int y, Tile tile)
        {
            Layer[y * Width + x] = tile;
        }

        public void SetPassTile(int x, int y, int tileIndex, List<Rectangle> passRects)
        {
            PassLayer[y * Width + x] = new PassTile(tileIndex, passRects);
        }

        public Tile GetTile(int x, int y)
        {
            return Layer[y * Width + x];
        }

        public PassTile GetPassTile(int x, int y)
        {
            return PassLayer[y * Width + x];
        }
    }
}